package com.example.demo.Controller.Topic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.Bean.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Entity.Opus.Opus;
import com.example.demo.ServiceImpl.Opus.OpusServiceImpl;
import com.example.demo.ServiceImpl.Topic.TopicServiceImpl;
import com.example.demo.Entity.Topic.Topic;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/topic")
public class TopicController {
    @Resource
    private OpusServiceImpl opusService;
    @Resource
    private TopicServiceImpl topicService;

    @PostMapping
    public Boolean save(@RequestBody Topic topic) {
        return topicService.saveOrUpdate(topic);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable(value = "id") Integer id) {
        return topicService.removeById(id);
    }

    @GetMapping
    public R findAll() {
        return R.ok().put("topicList", topicService.list());
    }

    @GetMapping("/{id}")
    public R findOne(@PathVariable(value = "id") Integer id) {
        LambdaQueryWrapper<Opus>opusLambdaQueryWrapper=new LambdaQueryWrapper<>();
        opusLambdaQueryWrapper.eq(Opus::getUid, id);
        Opus opus=opusService.getOne(opusLambdaQueryWrapper);
        if(opus==null){
            LambdaQueryWrapper<Topic>topicLambdaQueryWrapper=new LambdaQueryWrapper<>();
            topicLambdaQueryWrapper.orderByDesc(Topic::getId);
            topicLambdaQueryWrapper.eq(Topic::getIsselect,0);
            return R.ok().put("topic", topicService.list(topicLambdaQueryWrapper)).put("opus",opus);
        }
        return R.ok().put("topic", topicService.getById(opus.getTid())).put("opus",opus);
    }

    @GetMapping("/page")
    public R findPage(@RequestParam(value = "pageNum") Integer pageNum,
                      @RequestParam(value = "pageSize") Integer pageSize) {
        return R.ok().put("topicPage", topicService.page(new Page<>(pageNum, pageSize)));
    }

}